package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.Interest;
import com.huifaapp.mall.bean.Huishengqian.shareyouqu;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Umshares;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.socialize.bean.SHARE_MEDIA;

/*
转发
*/
public class Zhuang_FaActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mGvmineBack;
    private TextView mTotalProfit;
    private TextView mTotalPeople;
    private TextView mTotalRedMoney;
    private TextView mTotalUserProfit;
    private Button mFenxiang;
    private  PopupWindow window;
    private shareyouqu.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuang__fa);
        initview();
        getNetdata();
        getNetLj();



    }

    private void getNetLj() {
        String url = ApiManager.SHAREyouqu+ "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this);
        Log.e("转发链接", "主题url====" + url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                shareyouqu shareyouqu = new Gson().fromJson(responseInfo.result, shareyouqu.class);
                if (shareyouqu.getCode().equals("10000")){
                   data = shareyouqu.getData();
                   Log.e("lianjei",data.toString());
                }else {
                    Toast.makeText(Zhuang_FaActivity.this,shareyouqu.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(Zhuang_FaActivity.this,s,Toast.LENGTH_SHORT).show();

            }
        });


    }

    private void getNetdata() {
        String url = ApiManager.Interest+ "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this);
        Log.e("zhuanfa首页", "主题url====" + url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Interest interest = new Gson().fromJson(responseInfo.result, Interest.class);
                if (interest.getCode().equals("10000")){
                    Interest.DataBean data = interest.getData();
                    mTotalProfit.setText(data.getTotalProfit());
                    mTotalPeople.setText(data.getTotalPeople());
                    mTotalRedMoney.setText(data.getTotalRedMoney());
                    mTotalUserProfit.setText(data.getTotalUserProfit());

                }else {
                    Toast.makeText(Zhuang_FaActivity.this,interest.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(Zhuang_FaActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });


    }

    private void initview() {
        mGvmineBack = (ImageView) findViewById(R.id.Gvmine_back);
        mGvmineBack.setOnClickListener(this);
        mTotalProfit = (TextView) findViewById(R.id.totalProfit);
        mTotalPeople = (TextView) findViewById(R.id.totalPeople);
        mTotalRedMoney = (TextView) findViewById(R.id.totalRedMoney);
        mTotalUserProfit = (TextView) findViewById(R.id.totalUserProfit);
        mFenxiang = (Button) findViewById(R.id.fenxiang);
        mFenxiang.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fenxiang:
                View view1 =   LayoutInflater.from(Zhuang_FaActivity.this).inflate(R.layout.popwindownshangtan,null, false);
                window = new PopupWindow(view1, RelativeLayout.LayoutParams.MATCH_PARENT,600,true );
                window.setOutsideTouchable(true);
                window.setTouchable(true);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 0.7f;
                getWindow().setAttributes(lp);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1f;
                        getWindow().setAttributes(lp);
                    }
                });
                /*
                        分享朋友
                 */
                view1.findViewById(R.id.share_penyou).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Umshares.shareWeb(Zhuang_FaActivity.this,data.getUrl(),data.getTitle(),
                                data.getContent(),data.getImgUrl(),R.drawable.guanbi, SHARE_MEDIA.WEIXIN);

                    }
                });
                /*
                        分享朋友圈
                 */
                view1.findViewById(R.id.share_pengyouquna).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Umshares.shareWeb(Zhuang_FaActivity.this,data.getUrl(),data.getTitle(),
                                data.getContent(),data.getImgUrl(),R.drawable.guanbi, SHARE_MEDIA.WEIXIN_CIRCLE);

                    }
                });
                //关闭弹窗
                view1.findViewById(R.id.imgguanbi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                    }
                });
                window.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM , 0, 0);

                break;



            case R.id.Gvmine_back:
                finish();
                break;
        }
    }
}
